// pages/newsDetail/newsDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },


  //编辑消息
  setnews:function(e){
    var newss={
      newscontent:e.detail.value
    }
    //存储当条消息
    wx.setStorageSync('newss',newss)
  },

  //发送消息
  sendnews:function(){
    //获取当条消息
    let newss=wx.getStorageSync('newss')

    //获取发消息时间
    var util=require('../../pages/utils/util.js')
    newss.newstime=util.formatTime(new Date())

    newss.userid=this.data.id
    newss.friendid=this.data.friendid
    console.log(newss)

    //添加消息记录
    wx.request({
      url: `https://www.prebeauty.work:3001/api/newslist`,
      data:{
        userid:newss.userid,
        friendid:newss.friendid,
        newscontent:newss.newscontent,
        newstime:newss.newstime
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success:(res)=>{
        newss.newsid=res.data
        newss.newssend=1
        newss.newsfloat="left"

        console.log("news add success!")

        //本地添加消息
        let information=this.data.news
        information.push(newss)

        this.setData({
          news:information,
          inputValue:''
        });

        console.log(this.data.news)
      }
    })

    
  },

  //生命周期函数--监听页面加载
  onLoad: function (options) {
    this.setData({
      id: wx.getStorageSync('userid'),
      img:wx.getStorageSync('userimg'),
      name:wx.getStorageSync('username')
    })

    //好友信息
    this.setData({
      friendid:options.friendid
    })
    console.log(this.data)

    wx.request({
      url: `https://www.prebeauty.work:3001/api/userpage?userid=${this.data.friendid}`,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:(res)=>{
        console.log(res.data)
        this.setData({
          friendimg:res.data[0].userimg
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取消息记录
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/newslist',
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:(res)=>{
        console.log(res.data)
        let newslist=res.data
        let news=[]
        for(let i=0;i<newslist.length;i++){
          let time1=newslist[i].newstime.slice(0,10)
          let time2=newslist[i].newstime.slice(14,19)
          newslist[i].newstime=time1+' '+time2

          if(newslist[i].userid==this.data.id && newslist[i].friendid==this.data.friendid){
            newslist[i].newssend=1
            newslist[i].newsfloat="left"
            news.push(newslist[i])
          }
          if(newslist[i].friendid==this.data.id && newslist[i].userid==this.data.friendid){
            newslist[i].newssend=3
            newslist[i].newsfloat="right"
            news.push(newslist[i])
          }
        }
        this.setData({
          news:news
        })
        console.log(this.data.news)
      }
    })
  },

  //生命周期函数--监听页面隐藏
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //获取消息记录
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/newslist',
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:(res)=>{
        console.log(res.data)
        let newslist=res.data
        let news=[]
        for(let i=0;i<newslist.length;i++){
          let time1=newslist[i].newstime.slice(0,10)
          let time2=newslist[i].newstime.slice(14,19)
          newslist[i].newstime=time1+' '+time2

          if(newslist[i].userid==this.data.id && newslist[i].friendid==this.data.friendid){
            newslist[i].newssend=1
            newslist[i].newsfloat="left"
            news.push(newslist[i])
          }
          if(newslist[i].friendid==this.data.id && newslist[i].userid==this.data.friendid){
            newslist[i].newssend=3
            newslist[i].newsfloat="right"
            news.push(newslist[i])
          }
        }
        this.setData({
          news:news
        })
        console.log(this.data.news)
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
